﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
using ZhengMeiJiSettingUI.ModelEntity;
using ZhengMeiJiSettingUI.Common;
using System.Xml.Linq;

namespace ZhengMeiJiSettingUI.Data
{
    public class CKDServer
    {

        public async Task<IEnumerable<FHTZD>> GetFHTZDS(DateTime dateTime)
        {
            using (HttpClientHandler clientHandler = new HttpClientHandler())
            {
                clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
                using (HttpClient client = new HttpClient(clientHandler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Get,
                  $"{AppSettingsJson.GetAppSettings().GetSection("ConnectionUrl")["DefaultConnection"]}/api/TaskCreat/GetFHTZDS?dateTime={dateTime}");
                    request.Headers.Add("Accept", "text / plain");
                    //var client = _httpClientFactory.CreateClient();
                    try
                    {
                        var response = await client.SendAsync(request);
                        if (response.IsSuccessStatusCode)
                        {
                            using var responseStream = await response.Content.ReadAsStreamAsync();
                            var result = await JsonSerializer.DeserializeAsync
                                <IEnumerable<FHTZD>>(responseStream);
                            return result;
                        }
                        else
                        {
                            return null;
                        }

                    }
                    catch (Exception e)
                    {
                        return null;
                    }
                }
            }

        }


        public async Task<IEnumerable<XSCKD>> GetXSCKDS()
        {
            using (HttpClientHandler clientHandler = new HttpClientHandler())
            {
                clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
                using (HttpClient client = new HttpClient(clientHandler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Get,
                  $"{AppSettingsJson.GetAppSettings().GetSection("ConnectionUrl")["DefaultConnection"]}/api/TaskCreat/GetXSCKDS");
                    request.Headers.Add("Accept", "text / plain");
                    //var client = _httpClientFactory.CreateClient();
                    try
                    {
                        var response = await client.SendAsync(request);
                        if (response.IsSuccessStatusCode)
                        {
                            using var responseStream = await response.Content.ReadAsStreamAsync();
                            var result = await JsonSerializer.DeserializeAsync
                                <IEnumerable<XSCKD>>(responseStream);
                            return result;
                        }
                        else
                        {
                            return null;
                        }

                    }
                    catch (Exception e)
                    {
                        return null;
                    }
                }
            }
        }
        public async Task<IEnumerable<XSCKDLIST>> GetXSCKDLISTS()
        {
            using (HttpClientHandler clientHandler = new HttpClientHandler())
            {
                clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
                using (HttpClient client = new HttpClient(clientHandler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Get,
                   $"{AppSettingsJson.GetAppSettings().GetSection("ConnectionUrl")["DefaultConnection"]}/api/TaskCreat/GetXSCKDLISTS");
                    request.Headers.Add("Accept", "text / plain");
                    //var client = _httpClientFactory.CreateClient();
                    try
                    {
                        var response = await client.SendAsync(request);
                        if (response.IsSuccessStatusCode)
                        {
                            using var responseStream = await response.Content.ReadAsStreamAsync();
                            //这里返回结果改变
                            var result = await JsonSerializer.DeserializeAsync
                                <IEnumerable<XSCKDLIST>>(responseStream);
                            return result;
                        }
                        else
                        {
                            return null;
                        }

                    }
                    catch (Exception e)
                    {
                        return null;
                    }
                }
            }
        }


        public async Task<string> UpdatCKD(string FLOT,string custormer,string wl,string employeeName)
        {
            using (HttpClientHandler clientHandler = new HttpClientHandler())
            {
                clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
                using (HttpClient client = new HttpClient(clientHandler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Get,
                  $"{AppSettingsJson.GetAppSettings().GetSection("ConnectionUrl")["DefaultConnection"]}/api/TaskCreat/UpdataXSCKDS?FLOT={FLOT}&Custormer={custormer}&WLGS={wl}&userName={employeeName}");
                    request.Headers.Add("Accept", "text / plain");
                    //var client = _httpClientFactory.CreateClient();
                    try
                    {
                        var response = await client.SendAsync(request);
                        if (response.IsSuccessStatusCode)
                        {
                            var result = await response.Content.ReadAsStringAsync();
                            return result;

                        }
                        else
                        {
                            return "更新失败";
                        }

                    }
                    catch (Exception e)
                    {
                        return null;
                    }
                }
            }
        }

        public async Task<string> DeletXSCKD(string FID)
        {
            using (HttpClientHandler clientHandler = new HttpClientHandler())
            {
                clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
                using (HttpClient client = new HttpClient(clientHandler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Get,
                  $"{AppSettingsJson.GetAppSettings().GetSection("ConnectionUrl")["DefaultConnection"]}/api/TaskCreat/DeletXSCKD?FID={FID}");
                    request.Headers.Add("Accept", "text / plain");
                    //var client = _httpClientFactory.CreateClient();
                    try
                    {
                        var response = await client.SendAsync(request);
                        if (response.IsSuccessStatusCode)
                        {
                            var result = await response.Content.ReadAsStringAsync();
                            return result;

                        }
                        else
                        {
                            return null;
                        }

                    }
                    catch (Exception e)
                    {
                        return null;
                    }
                }
            }
        }

        public async Task<string> SaveXSCKDS(string FID, string FLOT, float FRealQty)
        {
            using (HttpClientHandler clientHandler = new HttpClientHandler())
            {
                clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
                using (HttpClient client = new HttpClient(clientHandler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Get,
                  $"{AppSettingsJson.GetAppSettings().GetSection("ConnectionUrl")["DefaultConnection"]}/api/TaskCreat/SaveXSCKDS?FID={FID}&FLOT={FLOT}&FRealQty={FRealQty}");
                    request.Headers.Add("Accept", "text / plain");
                    //var client = _httpClientFactory.CreateClient();
                    try
                    {
                        var response = await client.SendAsync(request);
                        if (response.IsSuccessStatusCode)
                        {
                            var result = await response.Content.ReadAsStringAsync();
                            return result;

                        }
                        else
                        {
                            return null;
                        }

                    }
                    catch (Exception e)
                    {
                        return null;
                    }
                }
            }

        }

        public async Task<IEnumerable<PDBlist>> GetPDBS()
        {
            using (HttpClientHandler clientHandler = new HttpClientHandler())
            {
                clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
                using (HttpClient client = new HttpClient(clientHandler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Get,
                  $"{AppSettingsJson.GetAppSettings().GetSection("ConnectionUrl")["DefaultConnection"]}/api/TaskCreat/GetPDBS");
                    request.Headers.Add("Accept", "text / plain");
                    //var client = _httpClientFactory.CreateClient();
                    try
                    {
                        var response = await client.SendAsync(request);
                        if (response.IsSuccessStatusCode)
                        {
                            using var responseStream = await response.Content.ReadAsStreamAsync();
                            var result = await JsonSerializer.DeserializeAsync
                                <IEnumerable<PDBlist>>(responseStream);
                            return result;
                        }
                        else
                        {
                            return null;
                        }

                    }
                    catch (Exception e)
                    {
                        return null;
                    }
                }
            }
        }

        public async Task<IEnumerable<PDB>> GetPDBNOUPdata(string FNumber)
        {
            using (HttpClientHandler clientHandler = new HttpClientHandler())
            {
                clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
                using (HttpClient client = new HttpClient(clientHandler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Get,
                  $"{AppSettingsJson.GetAppSettings().GetSection("ConnectionUrl")["DefaultConnection"]}/api/TaskCreat/GetPDDByNumberUnUpdata?FNumber={FNumber}");
                    request.Headers.Add("Accept", "text / plain");
                    //var client = _httpClientFactory.CreateClient();
                    try
                    {
                        var response = await client.SendAsync(request);
                        if (response.IsSuccessStatusCode)
                        {
                            using var responseStream = await response.Content.ReadAsStreamAsync();
                            var result = await JsonSerializer.DeserializeAsync
                                <IEnumerable<PDB>>(responseStream);
                            return result;
                        }
                        else
                        {
                            return null;
                        }

                    }
                    catch (Exception e)
                    {
                        return null;
                    }
                }
            }
        }

        public async Task<IEnumerable<XSCKD>> GetXSCKDNOUPdata(string zjm,string wlgs)
        {
            using (HttpClientHandler clientHandler = new HttpClientHandler())
            {
                clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
                using (HttpClient client = new HttpClient(clientHandler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Get,
                  $"{AppSettingsJson.GetAppSettings().GetSection("ConnectionUrl")["DefaultConnection"]}/api/TaskCreat/GetXSCKDsUnUpdata?ZJM={zjm}&WLGS={wlgs}");
                    request.Headers.Add("Accept", "text / plain");
                    //var client = _httpClientFactory.CreateClient();
                    try
                    {
                        var response = await client.SendAsync(request);
                        if (response.IsSuccessStatusCode)
                        {
                            using var responseStream = await response.Content.ReadAsStreamAsync();
                            var result = await JsonSerializer.DeserializeAsync
                                <IEnumerable<XSCKD>>(responseStream);
                            return result;
                        }
                        else
                        {
                            return null;
                        }

                    }
                    catch (Exception e)
                    {
                        return null;
                    }
                }
            }
        }
        public async Task<string> UpdataPdb(string FNumber, string Flot, float sum)
        {
            using (HttpClientHandler clientHandler = new HttpClientHandler())
            {
                clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
                using (HttpClient client = new HttpClient(clientHandler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Get,
                  $"{AppSettingsJson.GetAppSettings().GetSection("ConnectionUrl")["DefaultConnection"]}/api/TaskCreat/UpdataPD?FNumber={FNumber}&FLOT={Flot}&sum={sum}");
                    request.Headers.Add("Accept", "text / plain");
                    //var client = _httpClientFactory.CreateClient();
                    try
                    {
                        var response = await client.SendAsync(request);
                        if (response.IsSuccessStatusCode)
                        {
                            var result = await response.Content.ReadAsStringAsync();
                            return result;

                        }
                        else
                        {
                            return null;
                        }

                    }
                    catch (Exception e)
                    {
                        return null;
                    }
                }
            }
        }

        public async Task<string> UpdataADDPdb(string Number, string wlbm, string Flot, float sum)
        {
            using (HttpClientHandler clientHandler = new HttpClientHandler())
            {
                clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
                using (HttpClient client = new HttpClient(clientHandler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Get,
                  $"{AppSettingsJson.GetAppSettings().GetSection("ConnectionUrl")["DefaultConnection"]}/api/TaskCreat/UpdataADDPdb?Number={Number}&wlbm={wlbm}&Flot={Flot}&sum={sum}");
                    request.Headers.Add("Accept", "text / plain");
                    //var client = _httpClientFactory.CreateClient();
                    try
                    {
                        var response = await client.SendAsync(request);
                        if (response.IsSuccessStatusCode)
                        {
                            var result = await response.Content.ReadAsStringAsync();
                            return result;

                        }
                        else
                        {
                            return null;
                        }

                    }
                    catch (Exception e)
                    {
                        return null;
                    }
                }
            }
        }


        public async Task<string> submitPD(string FNumber)
        {
            using (HttpClientHandler clientHandler = new HttpClientHandler())
            {
                clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
                using (HttpClient client = new HttpClient(clientHandler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Get,
                  $"{AppSettingsJson.GetAppSettings().GetSection("ConnectionUrl")["DefaultConnection"]}/api/TaskCreat/submitPD?FNumber={FNumber}");
                    request.Headers.Add("Accept", "text / plain");
                    //var client = _httpClientFactory.CreateClient();
                    try
                    {
                        var response = await client.SendAsync(request);
                        if (response.IsSuccessStatusCode)
                        {
                            var result = await response.Content.ReadAsStringAsync();
                            return result;

                        }
                        else
                        {
                            return "调用接口失败";
                        }

                    }
                    catch (Exception e)
                    {
                        return "调用接口失败";
                    }
                }
            }
        }

        public async Task<string> submitXSCKD(string FID)
        {
            using (HttpClientHandler clientHandler = new HttpClientHandler())
            {
                clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
                using (HttpClient client = new HttpClient(clientHandler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Get,
                  $"{AppSettingsJson.GetAppSettings().GetSection("ConnectionUrl")["DefaultConnection"]}/api/TaskCreat/submitXSCKD?FID= {FID}");
                    request.Headers.Add("Accept", "text / plain");
                    //var client = _httpClientFactory.CreateClient();
                    try
                    {
                        var response = await client.SendAsync(request);
                        if (response.IsSuccessStatusCode)
                        {
                            var result = await response.Content.ReadAsStringAsync();
                            return result;

                        }
                        else
                        {
                            return "调用接口失败";
                        }

                    }
                    catch (Exception e)
                    {
                        return "调用接口失败";
                    }
                }
            }
        }
        public async Task<string> UpdataCW(string PH, string CW, string Area,string employeeName)
        {
            using (HttpClientHandler clientHandler = new HttpClientHandler())
            {
                clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
                using (HttpClient client = new HttpClient(clientHandler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Get,
                  $"{AppSettingsJson.GetAppSettings().GetSection("ConnectionUrl")["DefaultConnection"]}/api/TaskCreat/UpdataCW?PH={PH}&CW={CW}&Area={Area}&userName={employeeName}");
                    request.Headers.Add("Accept", "text / plain");
                    //var client = _httpClientFactory.CreateClient();
                    try
                    {
                        var response = await client.SendAsync(request);
                        if (response.IsSuccessStatusCode)
                        {
                            var result = await response.Content.ReadAsStringAsync();
                            return result;

                        }
                        else
                        {
                            return "调用接口失败";
                        }

                    }
                    catch (Exception e)
                    {
                        return "调用接口失败";
                    }
                }
            }

        }



    }
}
